package com.example.three.Controller.ShopController;


import com.example.three.DO.SysOfficeProd;
import com.example.three.DO.SysUser;
import com.example.three.DO.TableData;
import com.example.three.Service.SysShopService.SysShopService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller

@RequestMapping("")
public class ShopController {

    @Resource
    private SysShopService SysShopService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }


    @RequestMapping("To-shopgoodlist")
    public String ToRecom(){
        return "/shopgoodlist";
    }

    @RequestMapping("/shopgoodlist")
    @ResponseBody
    public TableData shopgoodlist(HttpSession session, Model model,@RequestParam Map<String, Object> params){
        String shopId = String.valueOf(session.getAttribute("shopid"));
        System.out.println(shopId);
        String prod_name=null;
        List<SysOfficeProd> sysOfficeProds = SysShopService.profindBy(shopId);
        if(params.containsKey("keyWord")){
            prod_name=params.get("keyWord").toString();
        }


        if (prod_name == null || prod_name == "") {
            session.setAttribute("shopgoodList",sysOfficeProds);
            model.addAttribute("shopgoodlist", sysOfficeProds);


        }
        else {
            sysOfficeProds = SysShopService.selectgood(shopId,prod_name);
            session.setAttribute("shopgoodList",sysOfficeProds);
            model.addAttribute("shopgoodlist", sysOfficeProds);
        }
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setData(sysOfficeProds);
        return tableData;
    }

    @RequestMapping("/addshopgood")
    public String addshopgood(HttpSession session){
        return "addshopgood";
    }

    @RequestMapping("/addshopgood1")
    public String addshopgood1(String proId,String proName,String proType,String isConsume,String price,
                              String pic,String status,String totalStocks,HttpSession session){
        String shopId = String.valueOf(session.getAttribute("shopid"));
        long proId1 = Long.parseLong(proId);
        SysShopService.addpro(proId1,proName,proType,shopId,isConsume,price,pic,status,totalStocks,0);
        return "redirect:/addshopgood";
    }

    @RequestMapping("/updateshopgood")
    public String updateshopgood(@RequestParam("id")long proId,HttpSession session){
        session.setAttribute("proId",proId);
        return "updateshopgood";
    }

    @RequestMapping("/updateshopgood1")
    public String updateshopgood1(String proName,String proType,String isConsume,String price,
                                 String pic,String status,String totalStocks,HttpSession session){
        String shopId = String.valueOf(session.getAttribute("shopid"));
        long proId = (long) session.getAttribute("proId");
        SysShopService.setpro(proId,proName,proType,shopId,isConsume,price,pic,status,totalStocks);
        return "redirect:/shopgoodlist";
    }

    @RequestMapping("/deleteshopgood")
    public String deleteshopgood(long prodId, HttpSession session){
        String shopId = String.valueOf(session.getAttribute("shopid"));
        SysShopService.prodelete(shopId,prodId);
        return "redirect:/shopgoodlist";
    }

//    已领用物品信息列表
//    @RequestMapping("/OfficeLog-list")
//    @ResponseBody
//    public TableData OfficeLogList(HttpSession session,@RequestParam Map<String, Object> params){
//        SysUser user = (SysUser)session.getAttribute("user");
//        String officeId = null;
//        String officeName=null;
//        String username = (String)session.getAttribute("teacher_name");
//        if (!params.isEmpty())
//        {
//            int page = Integer.parseInt(params.get("page").toString()),
//                    limit = Integer.parseInt(params.get("limit").toString());
//            params.remove("limit");
//            params.remove("page");
//            if(params.containsKey("keyWord"))
//            {
//
//                officeId = params.get("keyWord").toString();
//                officeName=params.get("keyWord").toString();
//
//            }
//
//        }
//        QueryWrapper<SysOfficeLog> queryWrapper = new QueryWrapper<>();
//        if (officeId== null || officeId == "" &&officeName == null || officeName == "") {
//            queryWrapper.eq("teacher_name",username);
//        }
//        TableData tableData = new TableData();
//        tableData.setMsg("查询成功");
//        tableData.setCode(0);
//        tableData.setData(sysOfficeLogList);
//        return tableData;
//
//
//    }

}
